/*	$Id: media.c 1123 2010-12-16 04:03:27Z phrakt $	*/
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


#include <sys/param.h>

#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include <canopy/log.h>
#include <canopy/mem.h>
#include <canopy/htab.h>
#include <canopy/string.h>

#include "media.h"
#include "private.h"




static const char http_media_default[] = "application/octet-stream";


struct http_media_type {
	char  *mt_type;
	char  *mt_exts[8];
};


/*
 * Top-Level Media Types
 *
 * The following table
 */
#if 0
static const char *http_media_toplvl_types[] = {
	"application",
	"audio",
	"image",
	"message",
	"multipart",
	"text",
	"video",
};
#endif


#define MEDIA_TYPECNT	(sizeof(http_media_toplvl_types) / sizeof(http_media_toplvl_types[0]))



static cnp_htab_t *http_media_htab;


/*
 * The <http_media_exthtab> hash table contains the media types indexed by
 * file extension.
 */
static cnp_htab_t *http_media_exthtab;



/*
 * http_media_init()
 *
 */
int
http_media_init(void)
{
	http_media_htab = cnp_htab_alloc(8, 0, NULL, NULL, NULL);
	if (http_media_htab == NULL) {
		http_log_err("failed to allocate media type "
		    "hash table");
		return (-1);
	}

	http_media_exthtab = cnp_htab_alloc(8, 0, NULL, NULL, NULL);
	if (http_media_exthtab == NULL) {
		http_log_err("failed to allocate media type "
		    "hash table");
		return (-1);
	}

	return (0);
}

/*
 * http_media_cleanup()
 *
 */
void
http_media_cleanup(void)
{
	cnp_htab_free(http_media_htab);
	http_media_htab = NULL;

	cnp_htab_free(http_media_exthtab);
	http_media_exthtab = NULL;
}

/*
 * http_media_addtype()
 *
 * Add a media type <type> to the table of known media types and associate it
 * with the file extensions found in the array <exts>, which contains
 * <extcnt> valid entries.
 */
int
http_media_addtype(const char *type, const char *ext[], int extcnt)
{
	unsigned int i;
	struct http_media_type *mt;

	if ((mt = cnp_malloc(sizeof(*mt))) == NULL) {
		http_log_err("failed to allocate media type entry");
		return (-1);
	}

	if ((mt->mt_type = cnp_strdup(type)) == NULL) {
		http_log_err("failed to copy media type");
		cnp_free(mt);
		return (-1);
	}

	for (i = 0; i < MIN(8, (unsigned int)extcnt); i++) {
		mt->mt_exts[i] = cnp_strdup(ext[i]);
	}

	cnp_htab_insert(http_media_htab, mt->mt_type, strlen(mt->mt_type), mt);

	for (i = 0; i < (unsigned int)extcnt; i++) {
		cnp_htab_insert(http_media_exthtab, mt->mt_exts[i],
		    strlen(mt->mt_exts[i]), mt);
	}

	return (0);
}


/*
 * http_media_getbyext()
 *
 * Get the MIME media type related to the file extension <ext> and store the
 * result in the destination buffer <dst>, which is of size <len>.
 * The buffer <ext> may contain a complete filename, in which case the
 * last extension of the filename is used.
 * Returns 1 if an exact match was found, 0 if the default type was set,
 * and -1 if an error occurred.
 */
int
http_media_getbyext(const char *ext, char *dst, size_t len)
{
	size_t extlen;
	const char *extp;
	struct http_media_type *mt;

	if ((extp = strrchr(ext, '.')) == NULL)
		extp = ext;
	else
		extp++;

	if ((extlen = strlen(extp)) > 8)
		;

	mt = cnp_htab_lookup(http_media_exthtab, extp, extlen);
	if (mt != NULL) {
		strlcpy(dst, mt->mt_type, len);
	} else
		strlcpy(dst, "application/octet-stream", len);

	return (0);
}

#if 0
/*
 * http_media_accepted()
 *
 * Check whether the media type <media> is part of the list of accepted media
 * types <list>.
 * Returns 1 if an exact match was found, 0 if the default type was set,
 * and -1 if an error occurred.
 */
int
http_media_accepted(const char *list, const char *type)
{
	return (0);
}
#endif
